<?php

/**
* ScoreBoardTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';

class  ScoreBoardTest extends WebTestCase
{

    private $dbh = '';
    private $username = '';
    private $password = '';


    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];

        $locale =  &PEAR::getStaticProperty('CONFIG_INI','LOCALE');
        $this->locale = $locale['default_locale']; 
        

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $this->dbh = new PDO($options['dsn_string']); 
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");
        $this->get($this->url.'?');
        AuthSingleton::instance('AUTH_USER')->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody');
        $this->assertText('Mapa poglądowa oraz lista trofeów');


    }

    public function testOneEntry()
    {
        $sth = $this->dbh->prepare('INSERT INTO guide (code, owner) VALUES (:code_id,:owner) ');
        $sth -> bindValue(':code_id','000001',PDO::PARAM_STR);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $idArray[] = $this->dbh->lastInsertId(); 

        $sth = $this->dbh->prepare('INSERT INTO guide_name (guide_id,name,locale) VALUES (:guide_id,:name,:locale) ');
        $sth -> bindValue(':guide_id', $idArray[0] ,PDO::PARAM_INT);
        $sth -> bindValue(':locale',  $this->locale ,PDO::PARAM_STR);
        $sth -> bindValue(':name',  'test' ,PDO::PARAM_STR);
        $sth->execute();

        $sth = $this->dbh->prepare('INSERT INTO hunted_code (code_id, owner, time) VALUES (:code_id,:owner,:time) ');
        $sth -> bindValue(':code_id',$idArray[0],PDO::PARAM_INT);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth -> bindParam(':time',  time() ,PDO::PARAM_INT);
        $sth->execute();

        $this->get($this->url.'?cmd=ScoreBoard');

        $this->assertPattern('/\d+.*'.$this->username.'.*1/'); 
               
    }

    public function testOneEntryFirstPosition()
    {
        $sth = $this->dbh->prepare('INSERT INTO guide (code, owner) VALUES (:code_id,:owner) ');
        $sth -> bindValue(':code_id','000001',PDO::PARAM_STR);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $idArray[] = $this->dbh->lastInsertId(); 

        $sth = $this->dbh->prepare('INSERT INTO guide_name (guide_id,name,locale) VALUES (:guide_id,:name,:locale) ');
        $sth -> bindValue(':guide_id', $idArray[0] ,PDO::PARAM_INT);
        $sth -> bindValue(':locale',  $this->locale ,PDO::PARAM_STR);
        $sth -> bindValue(':name',  'test' ,PDO::PARAM_STR);
        $sth->execute();
        

        $sth = $this->dbh->prepare('INSERT INTO hunted_code (code_id, owner, time) VALUES (:code_id,:owner,:time) ');
        $sth -> bindValue(':code_id',$idArray[0],PDO::PARAM_INT);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth -> bindValue(':time',  0 ,PDO::PARAM_INT);
        $sth->execute();

        $this->get($this->url.'?cmd=ScoreBoard');

        $this->assertPattern('/1.*'.$this->username.'.*1/'); 
               
    }

    public function testTwoEntriesDifferentNumOfCode()
    {

        $sth = $this->dbh->prepare('INSERT INTO guide (code, owner) VALUES (:code_id,:owner) ');
        $sth -> bindValue(':code_id','000001',PDO::PARAM_STR);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $idArray[] = $this->dbh->lastInsertId(); 

        $sth -> bindValue(':code_id','000002',PDO::PARAM_STR);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $idArray[] = $this->dbh->lastInsertId(); 

        $sth -> bindValue(':code_id','000003',PDO::PARAM_STR);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $idArray[] = $this->dbh->lastInsertId();

        $sth = $this->dbh->prepare('INSERT INTO guide_name (guide_id,name,locale) VALUES (:guide_id,:name,:locale) ');
        $sth -> bindValue(':guide_id', $idArray[0] ,PDO::PARAM_INT);
        $sth -> bindValue(':locale',  $this->locale ,PDO::PARAM_STR);
        $sth -> bindValue(':name',  'test' ,PDO::PARAM_STR);
        $sth->execute();
        $sth -> bindValue(':guide_id', $idArray[1] ,PDO::PARAM_INT);
        $sth -> bindValue(':locale',  $this->locale ,PDO::PARAM_STR);
        $sth -> bindValue(':name',  'test' ,PDO::PARAM_STR);
        $sth->execute();
        $sth -> bindValue(':guide_id', $idArray[2] ,PDO::PARAM_INT);
        $sth -> bindValue(':locale',  $this->locale ,PDO::PARAM_STR);
        $sth -> bindValue(':name',  'test' ,PDO::PARAM_STR);
        $sth->execute();


        $sth = $this->dbh->prepare('INSERT INTO hunted_code (code_id, owner, time) VALUES (:code_id,:owner,:time) ');
        $sth -> bindValue(':code_id',$idArray[0],PDO::PARAM_INT);
        $sth -> bindValue(':owner', 'first'.$this->username ,PDO::PARAM_STR);
        $sth -> bindValue(':time',  0 ,PDO::PARAM_INT);
        $sth->execute();

        $sth -> bindValue(':code_id',$idArray[1],PDO::PARAM_INT);
        $sth -> bindValue(':owner', 'first'.$this->username ,PDO::PARAM_STR);
        $sth -> bindValue(':time',  1 ,PDO::PARAM_INT);
        $sth->execute();

        $sth -> bindValue(':code_id',$idArray[2],PDO::PARAM_INT);
        $sth -> bindValue(':owner', 'first'.$this->username ,PDO::PARAM_STR);
        $sth -> bindValue(':time',  2 ,PDO::PARAM_INT);
        $sth->execute();

        $sth -> bindValue(':code_id',$idArray[1],PDO::PARAM_INT);
        $sth -> bindValue(':owner', 'second'.$this->username ,PDO::PARAM_STR);
        $sth -> bindValue(':time',  0 ,PDO::PARAM_INT);
        $sth->execute();

        $sth -> bindValue(':code_id',$idArray[2],PDO::PARAM_INT);
        $sth -> bindValue(':owner', 'second'.$this->username ,PDO::PARAM_STR);
        $sth -> bindValue(':time',  1 ,PDO::PARAM_INT);
        $sth->execute();

        $this->get($this->url.'?cmd=ScoreBoard');

        $this->assertPattern('/1.*'.'first'.$this->username.'.*3.*0:0:2/','This will pass if start_time is set to @0'); 
        $this->assertPattern('/2.*'.'second'.$this->username.'.*2.*0:0:1/','This will pass if start_time is set to @0'); 

    }

    public function testTwoEntriesSameNumOfCodeDifferentTime()
    {


        $sth = $this->dbh->prepare('INSERT INTO guide (code, owner) VALUES (:code_id,:owner) ');
        $sth -> bindValue(':code_id','000001',PDO::PARAM_STR);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $idArray[] = $this->dbh->lastInsertId(); 

        $sth -> bindValue(':code_id','000002',PDO::PARAM_STR);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $idArray[] = $this->dbh->lastInsertId(); 

        $sth -> bindValue(':code_id','000003',PDO::PARAM_STR);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $idArray[] = $this->dbh->lastInsertId();

        $sth = $this->dbh->prepare('INSERT INTO guide_name (guide_id,name,locale) VALUES (:guide_id,:name,:locale) ');
        $sth -> bindValue(':guide_id', $idArray[0] ,PDO::PARAM_INT);
        $sth -> bindValue(':locale',  $this->locale ,PDO::PARAM_STR);
        $sth -> bindValue(':name',  'test' ,PDO::PARAM_STR);
        $sth->execute();
        $sth -> bindValue(':guide_id', $idArray[1] ,PDO::PARAM_INT);
        $sth -> bindValue(':locale',  $this->locale ,PDO::PARAM_STR);
        $sth -> bindValue(':name',  'test' ,PDO::PARAM_STR);
        $sth->execute();
        $sth -> bindValue(':guide_id', $idArray[2] ,PDO::PARAM_INT);
        $sth -> bindValue(':locale',  $this->locale ,PDO::PARAM_STR);
        $sth -> bindValue(':name',  'test' ,PDO::PARAM_STR);
        $sth->execute();


        $sth = $this->dbh->prepare('INSERT INTO hunted_code (code_id, owner, time) VALUES (:code_id,:owner,:time) ');
        $sth -> bindValue(':code_id',$idArray[0],PDO::PARAM_INT);
        $sth -> bindValue(':owner', 'first'.$this->username ,PDO::PARAM_STR);
        $sth -> bindValue(':time',  0 ,PDO::PARAM_INT);
        $sth->execute();

        $sth -> bindValue(':code_id',$idArray[1],PDO::PARAM_INT);
        $sth -> bindValue(':owner', 'first'.$this->username ,PDO::PARAM_STR);
        $sth -> bindValue(':time',  1 ,PDO::PARAM_INT);
        $sth->execute();

        $sth -> bindValue(':code_id',$idArray[2],PDO::PARAM_INT);
        $sth -> bindValue(':owner', 'first'.$this->username ,PDO::PARAM_STR);
        $sth -> bindValue(':time',  2 ,PDO::PARAM_INT);
        $sth->execute();

        $sth -> bindValue(':code_id',$idArray[0],PDO::PARAM_INT);
        $sth -> bindValue(':owner', 'second'.$this->username ,PDO::PARAM_STR);
        $sth -> bindValue(':time',  0 ,PDO::PARAM_INT);
        $sth->execute();

        $sth -> bindValue(':code_id',$idArray[1],PDO::PARAM_INT);
        $sth -> bindValue(':owner', 'second'.$this->username ,PDO::PARAM_STR);
        $sth -> bindValue(':time',  1 ,PDO::PARAM_INT);
        $sth->execute();

        $sth -> bindValue(':code_id',$idArray[2],PDO::PARAM_INT);
        $sth -> bindValue(':owner', 'second'.$this->username ,PDO::PARAM_STR);
        $sth -> bindValue(':time',  10*3600+11*60+12 ,PDO::PARAM_INT);
        $sth->execute();

        $this->get($this->url.'?cmd=ScoreBoard');

        $this->assertPattern('/1.*'.'first'.$this->username.'.*3.*0:0:2/','This will pass if start_time is set to @0'); 
        $this->assertPattern('/2.*'.'second'.$this->username.'.*3.*10:11:12/','This will pass if start_time is set to @0'); 

    }

    public function tearDown() 
    {

        AuthSingleton::instance('AUTH_USER')->removeUser($this->username);

        $sth = $this->dbh->query("SELECT rowid FROM guide WHERE  owner='".$this->username."'");
        
        for($i=0; $result = $sth->fetch(); $i++){
            $this->dbh->exec("DELETE FROM guide_name WHERE guide_id='".$result['rowid']."'");
        }
        
        $this->dbh->exec("DELETE FROM guide WHERE owner='".$this->username."'");
        $this->dbh->exec("DELETE FROM hunted_code WHERE owner='".$this->username."'");
        $this->dbh->exec("DELETE FROM hunted_code WHERE owner='first".$this->username."'");
        $this->dbh->exec("DELETE FROM hunted_code WHERE owner='second".$this->username."'");
        $dbh = null;
        
    }
 

}
?>
